<template>
  <div class="app-container">
    <div class="nav">
      <h5>banner管理</h5>
      <el-button class="btn-add" type="primary" @click="handekAddbanner" size="mini">新增</el-button>
    </div>
    <div class="table-container">
      <el-table ref="productTable" :data="list" style="width: 100%" @selection-change="handleSelectionChange" v-loading="listLoading" border="">
        <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
        <el-table-column label="banner类别" align="center">
          <template slot-scope="scope">
            <!-- banner类型: 0商品促销广告,1市场推广页面 -->
            <p v-if="scope.row.type==0">商品促销广告</p>
            <p v-else>市场推广页面</p>
          </template>
        </el-table-column>
        <el-table-column label="链接地址" align="center">
          <template slot-scope="scope">{{scope.row.url}}</template>
        </el-table-column>
        <el-table-column label="图片预览" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.pic" alt="" style="width:50%;height:50%">
          </template>
        </el-table-column>
        <el-table-column label="禁用/启用" width="100" align="center">
          <template slot-scope="scope">
            <el-switch @change="handleShowStatusChange(scope.$index, scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.status"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <p>
              <el-button size="mini" @click="handleShowProduct(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              <el-button size="mini" @click="handleMoveupward(scope.$index, scope.row)">上移</el-button>
              <el-button size="mini" @click="handleMovedown(scope.$index, scope.row)">下移</el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination background="" @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes,prev, pager, next,jumper" :page-size="listQuery.pageSize" :page-sizes="[5,10,15]" :current-page.sync="listQuery.pageNum" :total="total"></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { fetchList, bannerDeleteStatus, updateShowStatus } from "@/api/banner";
// import {
//   fetchList,
//   // updateNewStatus,
//   // updateRecommendStatus,
//   // updatePublishStatus
// } from "@/api/product";
// import { fetchListWithChildren } from "@/api/productCate";
const defaultListQuery = {
  keywords: "",
  pageNum: 1,
  pageSize: 5,
  merchantId: 1
  // status: 0
};
export default {
  name: "productList",
  data() {
    return {
      value2: true,
      operateType: null,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,
      selectProductCateValue: null,
      multipleSelection: [],
      productCateOptions: [],
      brandOptions: [],
      publishStatusOptions: [
        {
          value: 1,
          label: "上架"
        },
        {
          value: 0,
          label: "下架"
        }
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: "审核通过"
        },
        {
          value: 0,
          label: "未审核"
        }
      ]
    };
  },
  created() {
    this.getList();
    // this.getBrandList();
    // this.getProductCateList();
  },
  watch: {
    selectProductCateValue: function(newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }
    }
  },
  filters: {
    verifyStatusFilter(value) {
      if (value === 1) {
        return "审核通过";
      } else {
        return "未审核";
      }
    }
  },
  methods: {
    getProductSkuSp(row, index) {
      if (index === 0) {
        return row.sp1;
      } else if (index === 1) {
        return row.sp2;
      } else {
        return row.sp3;
      }
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    // 启用、禁用
    handleShowStatusChange(index, row) {
      let data = new URLSearchParams();
      data.append("id", row.id);
      data.append("status", row.status);
      data.append("merchantId", 1);
      // updateShowStatus(data).then(response => {
      //   this.$message({
      //     message: "修改成功",
      //     type: "success",
      //     duration: 1000
      //   });
      //   this.getList();
      // });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getProductCateList() {
      fetchListWithChildren().then(response => {
        let list = response.data;
        this.productCateOptions = [];
        for (let i = 0; i < list.length; i++) {
          let children = [];
          if (list[i].children != null && list[i].children.length > 0) {
            for (let j = 0; j < list[i].children.length; j++) {
              children.push({
                label: list[i].children[j].name,
                value: list[i].children[j].id
              });
            }
          }
          this.productCateOptions.push({
            label: list[i].name,
            value: list[i].id,
            children: children
          });
        }
      });
    },
    handleShowSkuEditDialog(index, row) {
      this.editSkuInfo.dialogVisible = true;
      this.editSkuInfo.productId = row.id;
      this.editSkuInfo.productSn = row.productSn;
      this.editSkuInfo.productAttributeCategoryId =
        row.productAttributeCategoryId;
      this.editSkuInfo.keyword = null;
      fetchSkuStockList(row.id, { keyword: this.editSkuInfo.keyword }).then(
        response => {
          this.editSkuInfo.stockList = response.data;
        }
      );
      fetchProductAttrList(row.productAttributeCategoryId, { type: 0 }).then(
        response => {
          this.editSkuInfo.productAttr = response.data.list;
        }
      );
    },
    handleSearchEditSku() {
      fetchSkuStockList(this.editSkuInfo.productId, {
        keyword: this.editSkuInfo.keyword
      }).then(response => {
        this.editSkuInfo.stockList = response.data;
      });
    },

    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleAddProduct() {
      this.$router.push({ path: "/pms/addProduct" });
    },
    // handleBatchOperate() {
    //   if (this.operateType == null) {
    //     this.$message({
    //       message: "请选择操作类型",
    //       type: "warning",
    //       duration: 1000
    //     });
    //     return;
    //   }
    //   if (this.multipleSelection == null || this.multipleSelection.length < 1) {
    //     this.$message({
    //       message: "请选择要操作的商品",
    //       type: "warning",
    //       duration: 1000
    //     });
    //     return;
    //   }
    //   this.$confirm("是否要进行该批量操作?", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(() => {
    //     let ids = [];
    //     for (let i = 0; i < this.multipleSelection.length; i++) {
    //       ids.push(this.multipleSelection[i].id);
    //     }
    //     switch (this.operateType) {
    //       case this.operates[0].value:
    //         this.updatePublishStatus(1, ids);
    //         break;
    //       case this.operates[1].value:
    //         this.updatePublishStatus(0, ids);
    //         break;
    //       case this.operates[2].value:
    //         this.updateRecommendStatus(1, ids);
    //         break;
    //       case this.operates[3].value:
    //         this.updateRecommendStatus(0, ids);
    //         break;
    //       case this.operates[4].value:
    //         this.updateNewStatus(1, ids);
    //         break;
    //       case this.operates[5].value:
    //         this.updateNewStatus(0, ids);
    //         break;
    //       case this.operates[6].value:
    //         break;
    //       case this.operates[7].value:
    //         this.updateDeleteStatus(1, ids);
    //         break;
    //       default:
    //         break;
    //     }
    //     this.getList();
    //   });
    // },

    handleDelete(index, row) {
      this.$confirm("是否要进行删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = new URLSearchParams();
        params.append("id", row.id);
        bannerDeleteStatus(params).then(response => {
          this.$message({
            message: "删除成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
    handleUpdateProduct(index, row) {
      this.$router.push({ path: "/pms/updateProduct", query: { id: row.id } });
    },
    // 编辑
    handleShowProduct(index, row) {
      this.$router.push({ path: "/kkg/updateBanner", query: { id: row.id } });
      // this.editSkuInfo.dialogVisible = true;
      // console.log("handleShowProduct", row);
    },
    // 上移
    handleMoveupward() {},
    // 下移
    handleMovedown() {},
    // 新增
    handekAddbanner() {
      this.$router.push({ path: "banneradd" });
    },
    handleShowVerifyDetail(index, row) {
      console.log("handleShowVerifyDetail", row);
    },
    handleShowLog(index, row) {
      console.log("handleShowLog", row);
    },
    updatePublishStatus(publishStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("publishStatus", publishStatus);
      updatePublishStatus(params).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    updateNewStatus(newStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("newStatus", newStatus);
      updateNewStatus(params).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    updateRecommendStatus(recommendStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("recommendStatus", recommendStatus);
      updateRecommendStatus(params).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    updateDeleteStatus(deleteStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("deleteStatus", deleteStatus);
      updateDeleteStatus(params).then(response => {
        this.$message({
          message: "删除成功",
          type: "success",
          duration: 1000
        });
      });
      this.getList();
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
  background: #fff;
  min-height: 500px;
  .nav {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    h5 {
      font-size: 16px;
    }
    .btn-add {
      height: 48px;
      width: 80px;
    }
  }
}
</style>


